package com.tegtech.module.auth.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tegtech.module.auth.domain.SysRole;
import com.tegtech.module.auth.mapper.UserRolePermsMapper;
import com.tegtech.module.auth.service.IUserRolePermsService;

import cn.hutool.core.lang.Validator;

/**
 * 角色 业务层处理
 *
 * @author Jun
 */
@Service
public class UserRolePermsServiceImpl extends ServiceImpl<UserRolePermsMapper, SysRole> implements IUserRolePermsService {

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<SysRole> perms = baseMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms) {
            if (Validator.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

}
